<?php
header("Content-Type: text/html; charset=iso-8859-1");
require('include.php');
$tablename = (!empty($_REQUEST['tablename'])) ? $_REQUEST['tablename'] : '';
$table = (!empty($_SESSION['table'.$tablename])) ? $_SESSION['table'.$tablename] : "";
$mytable = unserialize($table);
if($mytable){
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : '';
	$value = (!empty($_REQUEST['value'])) ? $_REQUEST['value'] : "";	
	if($action == 'sort'){
		if($mytable->order == $value){
			$mytable->sense = ($mytable->sense == 'ASC') ? "DESC" : "ASC";
		}
		else{
			$mytable->order = $value;			
			$mytable->sense = "ASC";
		}
	}
	elseif($action == 'filter'){
		$mytable->filter_value = $value;
		$mytable->start = 1;
		foreach($mytable->rows as $row_id => $row){
			$mytable->rows[$row_id]->hidden = 0;			
			$found = 0;
			if($value){
				foreach($row->cols as $col){
					if(stripos($col,$value) !== false) $found = 1;
				}
				if(!$found){ $mytable->rows[$row_id]->hidden = 1; }
			}
		}
	}
	elseif($action == 'hide'){
		$status = (!empty($_REQUEST['status'])) ? $_REQUEST['status'] : "";	
		$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";	
		$col = (!empty($_REQUEST['col'])) ? $_REQUEST['col'] : "id";
		foreach($mytable->rows as $rid => $row){
			if($row->cols[$col] == $id){ $mytable->rows[$rid]->hidden = $status;}
		}
	}

	elseif($action == 'date'){
		$mytable->start = 1;
		$date_col = "";
		for($i = 0; $i < count($mytable->fields); $i++){
			if($mytable->fields[$i]->name == 'date'){
				$date_col = $i;
			}
		}
		if($date_col != ""){
			foreach($mytable->rows as $rid => $row){
				if(isset($row->cols)){
					$mytable->rows[$rid]->hidden = 0;			
					if($value == -1){
						if(strtotime($row->cols['date']) <= strtotime(date('Y-m-d'))){ $mytable->rows[$rid]->hidden = 1;}
					}
					elseif($value == 1){
						if(strtotime($row->cols['date']) > strtotime(date('Y-m-d'))) $mytable->rows[$rid]->hidden = 1;							
					}					
				}
			}			
		}
	}
	else{
		$start = (!empty($_REQUEST['start'])) ? $_REQUEST['start'] : 1;
		$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : '';
		$mytable->start = $start;
		$mytable->order = $order;		
	}
	echo $mytable->print_body();
	$_SESSION['table'.$tablename] = serialize($mytable);
}
?>